import mmap
import os

# 创建并预分配文件（如果不存在）
file_path = "data.bin"
file_size = 1024  # 1KB

if not os.path.exists(file_path):
    with open(file_path, "wb") as f:
        f.write(b'\x00' * file_size)  # 填充0

# 写入数据到共享内存
with open(file_path, "r+b") as f:


    # 映射整个文件到内存
    mm = mmap.mmap(f.fileno(), 0)

    # 写入字符串（需转为字节）
    data_to_write = "Hello from Process A"
    mm.seek(0)  # 移动指针到文件开头
    mm.write(data_to_write.encode())

    # 关闭映射和锁
    mm.close()
